package com.chen.controller;

import com.chen.dto.GradeDTO;
import com.chen.service.GradeService;
import com.chen.vo.GradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 年级管理相关接口
 */
@Controller
@RequestMapping("/grade")
public class GradeController {

    // 年级信息
    @Autowired
    private GradeService gradeService;

    /**
     * 获取所有年级集合
     * @return
     */
    @RequestMapping(value="/grades")
    public ModelAndView getGrades(ModelAndView model) {
        model.setViewName("admin/grades");
        // 年级列表信息
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        return model;
    }

    /**
     * 修改年级信息接口
     * @param gradeId
     * @return
     */
    @GetMapping("/update/{gradeId}")
    public ModelAndView update(@PathVariable("gradeId") Integer gradeId, ModelAndView model) {
        GradeVO grade = gradeService.findGradeById(gradeId);
        model.addObject("grade", grade);
        model.setViewName("/admin/gradeedit");
        return model;
    }

    /**
     * 修改年级信息
     * @param grade
     * @return
     */
    @PostMapping(value="/update")
    public String updateGrade(GradeDTO grade) {
        gradeService.updateGrade(grade);
        return "redirect:/grade/grades";
    }

    /**
     * 进入添加年级信息页面
     * @return
     */
    @GetMapping("/preAddGrade")
    public ModelAndView preAddGrade(ModelAndView model) {
        model.setViewName("/admin/gradeAdd");
        return model;
    }

    /**
     * 新增年级信息
     * @param grade
     * @return
     */
    @PostMapping(value="/save")
    public String save(GradeDTO grade) {
        gradeService.saveGrade(grade);
        return "redirect:/grade/grades";
    }

    /**
     * 删除年级
     * @param gradeId
     * @return
     */
    @PostMapping("/del/{gradeId}")
    public String delGrade(@PathVariable("gradeId") Integer gradeId) {
        gradeService.delGrade(gradeId);
        return "redirect:/grade/grades";
    }

}
